//
//  OtherMovieViewController.m
//  UI_豆瓣
//
//  Created by WangZe on 15/10/16.
//  Copyright © 2015年 王泽龙. All rights reserved.
//

#import "OtherMovieViewController.h"
#import "MovieList.h"
#import "MyCell.h"
#import "UIImageView+WebCache.h"
#import "NetWorkingTool.h"
#import "MovieDetailsViewController.h"
@interface OtherMovieViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, retain)UICollectionView *collectionView;
@property(nonatomic, retain)NSMutableArray *mvArr;

@end

@implementation OtherMovieViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"电影";
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(100, 200);
    
    flowLayout.minimumLineSpacing = 10;
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 30, 30, 20);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
    [self.view addSubview:self.collectionView];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[MyCell class] forCellWithReuseIdentifier:@"reuse"];
    
    
    [self createData];
}
- (void)createData
{
    self.mvArr = [NSMutableArray array];
    [NetWorkingTool getNetWorking:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php" block:^(id result) {
        NSDictionary *dic = result;
        NSArray *arr = dic[@"result"];
        for (NSDictionary *tempdic in arr) {
            MovieList *mv = [[MovieList alloc] init];
            [mv setValuesForKeysWithDictionary:tempdic];
            [self.mvArr addObject:mv];
        }
        [self.collectionView reloadData];
        //        self.hud.hidden = YES;
    }];
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MovieDetailsViewController *mvVC = [[MovieDetailsViewController alloc] init];
    [self.navigationController pushViewController:mvVC animated:YES];
    MovieList *mv = self.mvArr[indexPath.row];
    mvVC.movieID = mv.movieId;
    mvVC.tempMovie = mv;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.mvArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    MovieList *mv = self.mvArr[indexPath.row];
    
    [cell.mvimageView sd_setImageWithURL:[NSURL URLWithString:mv.pic_url]];
    cell.mvName.text = mv.movieName;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
